package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.api.GetAllActivityInfoAPI;
import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;
import com.eeepay.eeepay_v2.view.CustomChooseView;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.TitleBar;

import java.util.ArrayList;
import java.util.List;

import cn.eeepay.api.grpc.nano.MerchantSearchProto;
import cn.eeepay.api.grpc.nano.MerchantSearchServiceGrpc;
import cn.eeepay.api.grpc.nano.TerminalSerProto;
import io.grpc.ManagedChannel;

import static com.eeepay.eeepay_v2.util.Constant.PRODUCT_LIST;

/**
 * Created by zw on 2016/3/1 0001.
 * 卡片类型、 角色、 状态 、 产品  等选择框
 */
public class ChooseActivity extends ABBaseActivity implements View.OnClickListener {

//    private String[] states = {"贷记卡", "借记卡"};
    private List<String> datas;
    private TitleBar titleBar;
    private LinearLayout layout_content;

    private String intentFlag;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_card_type;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.title_bar);
//        titleBar.setShowRight(View.VISIBLE);
        //add by zhuangzeqin 2017年4月24日13:37:40 隐藏确定按钮
        titleBar.setShowRight(View.GONE);
        titleBar.setRightTextView("确定");
        titleBar.setRightTextColor(R.color.unify_grounding_white);
        layout_content = getViewById(R.id.layout_content);

        datas = new ArrayList<>();
        if(bundle != null){
            intentFlag = bundle.getString(Constant.INTENT_FLAG);
            if(Constant.PERSON_ROLE.equals(intentFlag)){
                datas.add("管理员");
                datas.add("销售员");
                initData();
                titleBar.setTiteTextView("角色");
            }else if(Constant.PERSON_STATUS.equals(intentFlag)){
                datas.add("正常");
                datas.add("失效");
                initData();
                titleBar.setTiteTextView("状态");
            }else if(PRODUCT_LIST.equals(intentFlag)){
                titleBar.setTiteTextView("业务产品");
                sendHttpRequest(0);
                return;
            }else if(Constant.CARD_TYPE.equals(intentFlag)){
                datas.add("贷记卡");
                datas.add("借记卡");
                titleBar.setTiteTextView("卡片类型");
                initData();
            }else if (Constant.ACTIVITY_TYPE.equals(intentFlag))//add by zhuangzeqin 2017年6月22日17:12:08 新增活动类型选择
            {
//                datas.add("循环送");
//                datas.add("移公社");
//                datas.add("返现");
//                datas.add("循环送-C");
//                datas.add("欢乐送");
                GetAllActivityInfoAPI getAllActivityInfoAPI =new GetAllActivityInfoAPI();
                getAllActivityInfoAPI.setResultCallBack(new GetAllActivityInfoAPI.ResultCallBack() {
                    @Override
                    public void onFail(String msg) {
                        dismissProgressDialog();
                        showToast(msg);
                    }

                    @Override
                    public void onSucess(List<TerminalSerProto.ActivityInfo> activityInfos) {
                        dismissProgressDialog();
                        for (int i = 0; i < activityInfos.size(); i++) {
                            TerminalSerProto.ActivityInfo activityInfo =activityInfos.get(i);
                            CustomChooseView chooseView = new CustomChooseView(mContext);
                            chooseView.setTitle(activityInfo.activityName);//活动类型名称
                            chooseView.setTag(i);//活动类型对应的值
                            chooseView.setTag(R.id.tag_first,activityInfo.activityValue);//活动类型对应的值
                            chooseView.setOnClickListener(ChooseActivity.this);
                            layout_content.addView(chooseView);
                        }
                        for (TerminalSerProto.ActivityInfo activityInfo : activityInfos) {
//
                        }
                    }
                });
                showProgressDialog();
                getAllActivityInfoAPI.getAllActivityInfo();
                titleBar.setTiteTextView("机具活动类型");
            }
        }


    }

    private void initData() {
        for (int i = 0; i < datas.size(); i++) {
            CustomChooseView chooseView = new CustomChooseView(mContext);
            chooseView.setTitle(datas.get(i));
            chooseView.setTag(i);
            chooseView.setOnClickListener(this);
            layout_content.addView(chooseView);
        }
    }

    @Override
    protected void eventOnClick() {
        //确定
        titleBar.setRightOnClickListener(new TitleBar.RightBtnOnClickListener() {
            @Override
            public void onRightClick(View view) {
                getChooseResult();
            }
        });
    }

    /**
     * 获取选中的接口
     */
    private void getChooseResult() {
        Intent intent = new Intent();
        for (int i = 0; i < layout_content.getChildCount(); i++) {
            CustomChooseView cv = (CustomChooseView) layout_content.getChildAt(i);
            if (cv.getIsSelected()) {
                if(PRODUCT_LIST.equals(intentFlag)){
                    intent.putExtra(Constant.BP_ID, (String)cv.getTag(R.id.tag_first));
                    intent.putExtra(Constant.BP_NAME, cv.getLeftTitle());
                }
                else if (Constant.ACTIVITY_TYPE.equals(intentFlag))//活动类型选择；返回活动类型对应的值
                {
                    intent.putExtra(intentFlag, cv.getLeftTitle());
                    intent.putExtra(Constant.ACTIVITY_VALUE, (String)cv.getTag(R.id.tag_first));
                }
                else {
                    intent.putExtra(intentFlag, cv.getLeftTitle());
                }
            }
        }
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void sendHttpRequest(int taskId) {
        showProgressDialog();
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT, taskId, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                MerchantSearchProto.BpdReq request = new MerchantSearchProto.BpdReq();
                request.agentNo = UserInfo.getUserInfo2SP().getAgentNo();
                MerchantSearchServiceGrpc.MerchantSearchServiceBlockingStub stub = MerchantSearchServiceGrpc.newBlockingStub(channel);
                MerchantSearchProto.BpdRes response = stub.getBpId(request);
                return response;
            }

            @Override
            public void updateUI(Object result, int taskId) {
                dismissProgressDialog();
                if(result == null){
                    showToast("网络错误");
                    return;
                }
                MerchantSearchProto.BpdRes response = (MerchantSearchProto.BpdRes) result;
                if(!response.success){
                    showToast(response.msg);
                    return;
                }
                MerchantSearchProto.PbdInfo[] infos = response.pbdInfo;
                for (int i = 0; i < infos.length; i++) {
                    CustomChooseView chooseView = new CustomChooseView(mContext);
                    chooseView.setTitle(infos[i].bpName);
                    chooseView.setTag(i);
                    chooseView.setTag(R.id.tag_first, infos[i].bpId);
                    chooseView.setOnClickListener(ChooseActivity.this);
                    layout_content.addView(chooseView);
                }

            }
        });
    }


    /**
     * 单选
     * @param index
     */
    private void singleSelect(int index){
        for (int i = 0; i < layout_content.getChildCount(); i++){
            CustomChooseView childView = (CustomChooseView) layout_content.getChildAt(i);
            if(i == index){
                childView.setIsSelected(true);
            }else{
                childView.setIsSelected(false);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if(v.getTag() != null){
            int index = (int) v.getTag();
            singleSelect(index);
        }
        getChooseResult();
    }
}
